<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use app\admin\model\HospitalLevelModel;
use app\admin\model\HospitalDepartModel;
use app\admin\model\HospitalDepartalModel;
use app\admin\model\HospitalListModel;
class HospitalController extends AdminBaseController
{
	/*
    	*医院等级列表页面
    */
    public function hoslevel()
    {

      	$levelinfo=Db::name('hospital_level')->select()->toArray();
      	foreach($levelinfo as $k=>$v){
      		if($v['agentid'] == 0)
      		{
      			$levelinfo[$k]['agentid'] = "无上级";
      		}
      		$agentinfo=Db::name('hospital_level')->where('id',$v['agentid'])->select()->toArray();

      	   foreach ($agentinfo as $key => $value) {
      	      $levelinfo[$k]['agentid'] = $value['level'];

      	   }
      	}

     return $this->fetch('Hospital/levelindex',[
        	'levelinfo'=>$levelinfo
        ]);




    }


  	/*
    	*医院等级添加
    */
	public function hosleveladd(){
		$levelinfo=Db::name('hospital_level')->where('agentid','0')->select();
		return $this->fetch('Hospital/leveladd',[
        	'levelinfo'=>$levelinfo
        ]);
    }

  	/*
    	*医院等级添加执行
    */
	public function hosleveladddo(){
    	if ($this->request->isPost()) {
        	  $data = $this->request->param();
          	  $level=new HospitalLevelModel();
          	  $res=$level->where('level',$data['level'])->find();
          	  if($res == NULL){
          	    	$level->agentid = $data['parent_id'];
              		$level->level = $data['level'];
                	$level->create_time = time();
                	$add=$level->save();
                	if($add == 1){
                    	$this->success('添加成功');
                    }else{
                    	$this->error('添加失败');
                    }

              }else{
              		$this->error('添加失败');
              }
        }
    }
     /*
    	*医院等级删除
    */
    public function hosleveldelete(){
        $data = $this->request->param();
        $res=HospitalLevelModel::destroy($data['id']);
    	if($res ==1){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}

    }
     /*
    	*医院等级修改
    */
    public function hosleveledit(){
       $data = $this->request->param();
       $res=HospitalLevelModel::where('id',$data['id'])->find();
       return $this->fetch('Hospital/leveledit',[
        		'res'=>$res['id'],'name'=>$res['level']
        	]);
    }
    public function hosleveleditt(){
      $data = $this->request->param();
      $time = time();
      $level=new HospitalLevelModel();
      $add=$level->where('id',$data['id'])->update(['create_time'=>$time,'level'=>$data['level']]);
      if($add == 1)
      {
      	$this->success('修改成功');
      }else{
    	$this->error('修改失败');
      }


    }

      /*
    	*医院部门列表页面
    */
     public function hosdepartment()
    {
    	$partmentinfo=Db::name('hospital_department')->select();
    	return $this->fetch('Hospital/departmentindex',[
        	'levelinfo'=>$partmentinfo
        ]);

    }
   /*
    	*医院部门删除
    */
       public function hosdepartdelete(){
        $data = $this->request->param();
        $res=HospitalDepartModel::destroy($data['id']);
    	if($res ==1){
    		$this->success('删除成功');
    	}else{
    		$this->error('添加失败');
    	}

    }
   	/*
    	*医院部门添加
    */
	public function hosdepartadd(){
		return $this->fetch('Hospital/departmentadd');
    }
    /*
    	*医院部门添加执行
    */
	public function hosdepartadddo(){

        	  $data = $this->request->param();
          	  $level=new HospitalDepartModel();
          	  $res=$level->where('name',$data['depart'])->find();
          	  if($res == NULL){
              		$level->name = $data['depart'];
                	$level->create_time = time();
                	$add=$level->save();
                	if($add == 1){
                    	$this->success('添加成功');
                    }else{
                    	$this->error('添加失败');
                    }

              }else{
              		$this->error('添加失败');
              }

    }
    /*
    	*医院部门修改
    */
    public function hosdepartedit(){
       $data = $this->request->param();
       $res=HospitalDepartModel::where('id',$data['id'])->find();
       return $this->fetch('Hospital/departmentedit',[
        		'res'=>$res['id'],'name'=>$res['name']
        	]);

    }
      public function hosdeparteditt(){
	      $data = $this->request->param();
	      $time = time();
	      $level=new HospitalDepartModel();
	      $add=$level->where('id',$data['id'])->update(['create_time'=>$time,'name'=>$data['edit']]);
	      if($add == 1)
	      {
	      	$this->success('修改成功');
	      }else{
	    	$this->error('修改失败');
	      }


    }

    /*
    	*医院科室列表页面
    */
     public function hosdepartmental()
    {
        $partmentinfo=Db::name('hospital_departmental')->select();
    	return $this->fetch('Hospital/departmentalindex',[
        	'levelinfo'=>$partmentinfo
        ]);
    }
     /*
    	*医院科室删除
    */
       public function hosdepartaldelete(){
	        $data = $this->request->param();
	        $res=HospitalDepartalModel::destroy($data['id']);
	    	if($res ==1){
	    		$this->success('删除成功');
	    	}else{
	    		$this->error('添加失败');
	    	}
    }

      /*
    	*医院科室添加执行
    */
     public function hosdepartaladd()
    {
    	return $this->fetch('Hospital/departmentaladd');
    }
      /*
    	*医院科室添加执行
    */
	public function hosdepartaladddo(){
      $data = $this->request->param();
      $level=new HospitalDepartalModel();
      $res=$level->where('name',$data['depart'])->find();
        if($res == NULL){
            $level->name = $data['depart'];
           	$level->create_time = time();
           	$add=$level->save();
           	if($add == 1){
               	$this->success('添加成功');
            }else{
               	$this->error('添加失败');
             }

         }else{
         		$this->error('添加失败');
       }

    }
       /*
    	*医院科室修改
    */
    public function hosdepartaledit(){
       $data = $this->request->param();
       $res=HospitalDepartalModel::where('id',$data['id'])->find();
       return $this->fetch('Hospital/departmentaledit',[
        		'res'=>$res['id'],'name'=>$res['name']
        	]);

    }
      public function hosdepartaleditt(){
	      $data = $this->request->param();
	      $time = time();
	      $level=new HospitalDepartalModel();
	      $add=$level->where('id',$data['id'])->update(['create_time'=>$time,'name'=>$data['edit']]);
	      if($add == 1)
	      {
	      	$this->success('修改成功');
	      }else{
	    	$this->error('修改失败');
	      }


    }
          /*
    	*医院列表页面
    */
    //医院筛选
     public function screen()
    {
        $data = $this->request->param();
        if($data['keyword'] != "")
        {

        	$map['name'] = array('like', '%' . $data['keyword'] . '%');


            $listinfo=Db::name('hospital_list')->where($map)->select()->toArray();
        }else if($data['category'] !=""){
	        $whereArr['agentid'] =  array('neq',0); // 不等于条件
	        $listinfo=Db::name('hospital_list')->where('status',$data['category'])->select()->toArray();
	        $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
	        foreach($listinfo as $k=>$v)
	        {
	            foreach($levelinfo as $ke=>$ve)
	            {
	               if($ve['id'] == $v['level'])
	               {
	                $listinfo[$k]['level'] = $ve['level'];
	                }
	             }
	         }
        }else{
        	 $whereArr['agentid'] =  array('neq',0); // 不等于条件
			 $listinfo=Db::name('hospital_list')->select()->toArray();
			 $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
			 foreach($listinfo as $k=>$v)
			  {
			      foreach($levelinfo as $ke=>$ve)
			      {
			         if($ve['id'] == $v['level'])
			         {
			         	$listinfo[$k]['level'] = $ve['level'];
			          }
			       }
		     }
        }
        return $this->fetch('Hospital/list',[
        	'listinfo'=>$listinfo
        ]);

    }

     public function hoslist()
    {

       $whereArr['agentid'] =  array('neq',0); // 不等于条件
	   $listinfo=Db::name('hospital_list')->select()->toArray();
	   $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
	   foreach($listinfo as $k=>$v)
	    {
	        foreach($levelinfo as $ke=>$ve)
	        {
	           if($ve['id'] == $v['level'])
	           {
	         	$listinfo[$k]['level'] = $ve['level'];
	            }
	         }
	     }

        return $this->fetch('Hospital/list',[
        	'listinfo'=>$listinfo
        ]);

    }
         /*
    	*医院列表添加页面
    */
     public function hoslistadd()
    {
    	$partmentinfo=Db::name('hospital_department')->select()->toArray();//部门
    	$departinfo=Db::name('hospital_departmental')->select()->toArray();//科室
    	$whereArr['agentid'] =  array('neq',0); // 不等于条件
    	$levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
        return $this->fetch('Hospital/listadd',[
        	'levelinfo'=>$levelinfo,'partmentinfo'=>$partmentinfo,'departinfo'=>$departinfo
        ]);
    }
     public function hoslistadddo()
    {
    	  $data = $this->request->param();
          $level=new HospitalListModel();
          $comma_separated = implode(",", $data['checkbox']);
          $comma_separated1 = implode(",", $data['checkbox1']);
          $res=$level->where('name',$data['name'])->find();
          if($res == NULL){
            $level->name = $data['name'];
            $level->city = $data['city'];
            $level->address = $data['address'];
            $level->status = $data['status'];
            $level->level = $data['level'];
            $level->depart = $comma_separated;
            $level->departal = $comma_separated1;
           	$level->create_time = time();
           	$add=$level->save();
           	if($add == 1){
               	$this->success('添加成功');
            }else{
               	$this->error('添加失败');
             }

         }else{
         		$this->error('添加失败');
       }
    }

    /*
    	*医院删除
    */
       public function hoslistdelete(){
	        $data = $this->request->param();
	        $res=HospitalListModel::destroy($data['id']);
	    	if($res ==1){
	    		$this->success('删除成功');
	    	}else{
	    		$this->error('添加失败');
	    	}
    }
      /*
    	*医院修改
    */
    public function hoslistedit(){
       $partmentinfo=Db::name('hospital_department')->select()->toArray();//部门
       $departinfo=Db::name('hospital_departmental')->select()->toArray();//科室
       $data = $this->request->param();
       $whereArr['agentid'] =  array('neq',0); // 不等于条件
       $levelinfo=Db::name('hospital_level')->where($whereArr)->select()->toArray();
       $res=HospitalListModel::where('id',$data['id'])->find();
       return $this->fetch('Hospital/listedit',[
        		'res'=>$res,'levelinfo'=>$levelinfo,'partmentinfo'=>$partmentinfo,'departinfo'=>$departinfo
        	]);

    }
     public function hoslisteditt(){
	      $data = $this->request->param();
	      $time = time();
	      $comma_separated = implode(",", $data['checkbox']);
        $comma_separated1 = implode(",", $data['checkbox1']);
	      $level=new HospitalListModel();
	      $add=$level->where('id',$data['id'])->update(['create_time'=>$time,'name'=>$data['name'],'level'=>$data['level'],'address'=>$data['address'],'city'=>$data['city'],'status'=>$data['status'],'depart'=>$comma_separated,'departal'=>$comma_separated1]);
	      if($add == 1)
	      {
	      	$this->success('修改成功');
	      }else{
	    	$this->error('修改失败');
	      }


    }



}

